<?php

namespace App\Http\Model;

/**
 * 用户发送的连击礼物数量
 *
 * Created on 2021/12/30 18:06
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class GiftDoubleHitStatistics extends Base
{
    /**
     * 表名字
     *
     * @var string
     */
    protected $table = 'gift_double_hit_statistics';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'user_id', 'gift_id', 'num', 'level', 'created_at', 'updated_at',];

    /**
     * 构造函数
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 更新已发送的礼物数量
     *
     * Created at 2022/1/8 10:31
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId 用户user_id
     * @param int $giftId 礼物gift_id
     * @param int $num 发送的礼物数量
     */
    public function incSentGiftNum(int $userId, int $giftId, int $num = 1)
    {
        if ($userId <= 0 || $giftId <= 0 || $num <= 0) {
            return;
        }

        $where = ['user_id' => $userId, 'gift_id' => $giftId];
        if (self::query()->where($where)->count()) {
            self::query()->where($where)->increment('num', $num);
        } else {
            self::query()->create(array_merge($where, ['num' => $num, 'level' => 1]));// 默认1级
        }
    }

    /**
     * 更新用户礼物佩戴级别
     *
     * Created at 2022/1/8 10:33
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId 用户user_id
     * @param int $giftId 礼物gift_id
     * @param int $level 升级礼物的等级, 正整数
     */
    public function updateGiftWearLevel(int $userId, int $giftId, int $level)
    {
        if ($userId <= 0 || $giftId <= 0 || $level <= 0) {
            return;
        }

        $where = ['user_id' => $userId, 'gift_id' => $giftId];
        self::query()->where($where)->update(['level' => $level]);
    }

    /**
     * 获取用户礼物佩戴等级
     *
     * Created at 2022/1/8 10:51
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId 用户user_id
     * @param int $giftId 礼物gift_id
     * @return int
     */
    public function getUserGiftWearLevel(int $userId, int $giftId): int
    {
        $where = ['user_id' => $userId, 'gift_id' => $giftId];
        $level = self::query()->where($where)->value('level');
        return $level ?: 1;
    }
    /**
     * 获取用户礼物佩戴等级礼物信息
     *
     * Created at 2022/1/8 10:51
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId 用户user_id
     * @param int $giftId 礼物gift_id
     * @return int
     */
    public function getUserGiftInfo(int $userId, int $giftId): array
    {
        $where = ['user_id' => $userId, 'gift_id' => $giftId];
        $levelInfo = self::query()->where($where)->select($this->fillable)->first()->toArray();
        return $levelInfo;
    }

    /**
     * 获取用户连点礼物统计数据
     *
     * Created at 2022/1/8 10:59
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $userId 用户user_id
     * @return array
     */
    public function listRecord(int $userId): array
    {
        if ($userId <= 0) {
            return [];
        }

        return self::query()->select(['gift_id', 'level'])->where('user_id', $userId)->get()->toArray();
    }
}